<?php 
//链接数据库
require('./lib/init.php');
//检测是否登录,有操作权限
tiao();

$cat_id = $_GET['id'];

//判断是否有post数据传过来
if (empty($_POST)) {
	//查询出原来的catname
	$sql = "select catname from cat where cat_id = '$cat_id'";
	$row = mysql_fetch_assoc(mQuery($sql));//取出的是一个关联数组
    include(ROOT.'/view/admin/catdit.html');
} else {
	$catname = trim($_POST['catname']);
	//判断栏目名称是否为空
	if ($catname == '') {
		error('栏目名不能为空');
	}
    //判断栏目名是否重复
    $sql = "select count(*) from cat where catname = '$catname' ";
    
    if (mGetOne($sql) != 0) {
    	error('栏目名已存在');
    }
	//修改操作
	$sql = "update cat set catname = '$catname' where cat_id = $cat_id";
	
	if (!mQuery($sql)) {
		echo mysql_error();
	}else {
		header('Location:catlist.php');
	}
}

 ?>